package com.simulation.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simulation.common.utils.SecurityUtils;
import com.simulation.common.utils.bean.BeanUtils;
import com.simulation.web.domain.TechCollectionPost;
import com.simulation.web.dto.TechCollectionDTO;
import com.simulation.web.domain.TechCollection;
import com.simulation.web.mapper.CollectionMapper;
import com.simulation.web.mapper.CollectionPostMapper;
import com.simulation.web.service.CollectionPostService;
import com.simulation.web.service.CollectionService;
import com.simulation.web.utils.PostStatusEnum;
import com.simulation.web.vo.TechCollectionVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.simulation.common.utils.SecurityUtils.getLoginUser;

@Service
@AllArgsConstructor
public class CollectionServiceImpl  implements CollectionService {

    private final CollectionMapper collectionMapper;

    private final CollectionPostService collectionPostService;

    private final CollectionPostMapper collectionPostMapper;


    @Override
    public PageInfo<TechCollectionVO> getPage(TechCollectionDTO techCollectionDTO) {
        PageHelper.startPage(techCollectionDTO.getPageNum(), techCollectionDTO.getPageSize());
        List<String> statusList = new ArrayList<>();
        if (StrUtil.isBlank(techCollectionDTO.getStatus())) {
            statusList.add(PostStatusEnum.PRIVATE.name().toLowerCase());
            statusList.add(PostStatusEnum.PUBLISHED.name().toLowerCase());
            statusList.add(PostStatusEnum.DRAFT.name().toLowerCase());
        } else {
            statusList.addAll(Arrays.asList(techCollectionDTO.getStatus().split(",")));
        }
        List<TechCollectionVO> collectionList = collectionMapper.getPage(techCollectionDTO, statusList);
        return new PageInfo<>(collectionList);
    }

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @Override
    public List<TechCollection> listAll() {
    	return collectionMapper.listAll();
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @Override
    public TechCollectionVO getById(Long id) {
    	return collectionMapper.getById(id);
    }
	
    /**
     * 新增，插入所有字段
     *
     * @param techCollection 新增的记录
     * @return 返回影响行数
     */
    @Override
    public int insert(TechCollection techCollection) {
    	return collectionMapper.insert(techCollection);
    }
	
    /**
     * 新增，忽略null字段
     *
     * @param techCollection 新增的记录
     * @return 返回影响行数
     */
    @Override
    public int insertIgnoreNull(TechCollection techCollection) {
        techCollection.setCreateTime(new Date());
        techCollection.setUserId(SecurityUtils.getUserId());
    	return collectionMapper.insertIgnoreNull(techCollection);
    }
	
    /**
     * 修改，修改所有字段
     *
     * @param techCollection 修改的记录
     * @return 返回影响行数
     */
    @Override
    public int update(TechCollection techCollection) {
    	return collectionMapper.update(techCollection);
    }
	
    /**
     * 修改，忽略null字段
     *
     * @param techCollection 修改的记录
     * @return 返回影响行数
     */
    @Override
    public int updateIgnoreNull(TechCollection techCollection) {
    	return collectionMapper.updateIgnoreNull(techCollection);
    }
	
    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        // 删除项目与集合的关联关系
        collectionPostService.delete(id, null);
        // 删除集合
    	return collectionMapper.delete(id);
    }

    @Override
    public List<TechCollectionVO> selectViewCount() {
        return collectionMapper.selectViewCount();
    }

    @Override
    public Boolean copy(Long id) {
        TechCollectionVO techCollectionVO = collectionMapper.getById(id);
        TechCollection techCollection = new TechCollection();
        BeanUtils.copyProperties(techCollectionVO, techCollection);
        techCollection.setId(null);
        techCollection.setSource(id.intValue());
        techCollection.setCreateTime(new Date());
        techCollection.setUserId(getLoginUser().getUserId());
        techCollection.setViewCount(0);
        techCollection.setCommentCount(0);
        techCollection.setFavoriteCount(0);
        techCollection.setFollowCount(0);
        collectionMapper.insert(techCollection);
        List<TechCollectionPost> techCollectionPosts = collectionPostService.getListByCollectionId(id);
        List<Long> postIds = techCollectionPosts.stream().map(TechCollectionPost::getPostId).collect(Collectors.toList());
        collectionPostService.insert(techCollection.getId(), postIds);
        return true;
    }
}